package com.luck.picture.lib.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.luck.picture.lib.R;

/**
 * @author wenchao
 * @date 2018/10/24
 * @time 14:16
 */
public class CommonDialog extends Dialog {

    private Context context;
    private String content;
    private String cancelStr;
    private String ensureStr;
    private DialogEnsureClickListener ensureClickListener;
    private DialogCancelClickListener cancelClickListener;
    private boolean isOnlyEnsure;

    private CommonDialog(Context context, String content, String cancelStr, String ensureStr,
                         DialogEnsureClickListener ensureClickListener,
                         DialogCancelClickListener cancelClickListener, boolean isOnlyEnsure) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.content = content;
        this.cancelStr = cancelStr;
        this.ensureStr = ensureStr;
        this.ensureClickListener = ensureClickListener;
        this.cancelClickListener = cancelClickListener;
        this.isOnlyEnsure = isOnlyEnsure;
    }

    public CommonDialog(Context context, String content, DialogEnsureClickListener ensureClickListener) {
        this(context, content, "取消", "确定", ensureClickListener, null, false);
    }

    public CommonDialog(Context context, String content, DialogEnsureClickListener ensureClickListener,
                        DialogCancelClickListener cancelClickListener,
                        String cancelStr, String ensureStr) {
        this(context, content, cancelStr, ensureStr, ensureClickListener, cancelClickListener, false);
    }

    public CommonDialog(Context context, String content, DialogEnsureClickListener ensureClickListener,
                        String cancelStr, String ensureStr) {
        this(context, content, cancelStr, ensureStr, ensureClickListener, null, false);
    }

    public CommonDialog(Context context, String content, DialogEnsureClickListener ensureClickListener,
                        boolean isOnlyEnsure) {
        this(context, content, "取消", "确定", ensureClickListener, null, isOnlyEnsure);
    }

    public CommonDialog(Context context, String content, boolean isOnlyEnsure) {
        this(context, content, "取消", "确定", null, null, isOnlyEnsure);
    }

    public CommonDialog(Context context, String content, DialogEnsureClickListener ensureClickListener,
                        String ensureStr, boolean isOnlyEnsure) {
        this(context, content, "取消", ensureStr, ensureClickListener, null, isOnlyEnsure);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context, R.layout.dialog_common, null);
        TextView tvContent = view.findViewById(R.id.tv_content);
        TextView tvEnsure = view.findViewById(R.id.tv_ensure);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        View dividingLine = view.findViewById(R.id.dividing_line);
        setContentView(view);
        tvContent.setText(content);
        tvCancel.setText(cancelStr);
        tvEnsure.setText(ensureStr);
        if (isOnlyEnsure) {
            tvCancel.setVisibility(View.GONE);
            dividingLine.setVisibility(View.GONE);
        }
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (cancelClickListener != null) {
                    cancelClickListener.onCancelClick();
                }
            }
        });
        tvEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (ensureClickListener != null) {
                    ensureClickListener.onEnsureClick();
                }
            }
        });
    }

    public interface DialogEnsureClickListener {

        void onEnsureClick();
    }

    public interface DialogCancelClickListener {

        void onCancelClick();
    }
}
